package roaringfunc difference( []uint16, []uint16, []uint16) int {iflen() == 0 { = [:len()]copy(, )returnlen() }iflen() == 0 {return0 } := 0 := 0 := 0 = [:cap()] := [] := []for {if < { [] = ++ ++if >= len() {break } = [] } elseif == { ++ ++if >= len() {break } = []if >= len() {for ; < len(); ++ { [] = [] ++ }break } = [] } else { // if (val1>val2) ++if >= len() {for ; < len(); ++ { [] = [] ++ }break } = [] } }return}func exclusiveUnion2by2( []uint16, []uint16, []uint16) int {if0 == len() { = [:len()]copy(, [:])returnlen() }if0 == len() { = [:len()]copy(, [:])returnlen() } := 0 := 0 := 0 := [] := [] = [:cap()]for {if < { [] = ++ ++if >= len() {for ; < len(); ++ { [] = [] ++ }break } = [] } elseif == { ++ ++if >= len() {for ; < len(); ++ { [] = [] ++ }break }if >= len() {for ; < len(); ++ { [] = [] ++ }break } = [] = [] } else { // if (val1>val2) [] = ++ ++if >= len() {for ; < len(); ++ { [] = [] ++ }break } = [] } }return}// union2by2Cardinality computes the cardinality of the unionfunc union2by2Cardinality( []uint16, []uint16) int { := 0 := 0 := 0if0 == len() {returnlen() }if0 == len() {returnlen() } := [] := []for {if < { ++ ++if >= len() { += len() - break } = [] } elseif == { ++ ++ ++if >= len() { += len() - break }if >= len() { += len() - break } = [] = [] } else { // if (set1[k1]>set2[k2]) ++ ++if >= len() { += len() - break } = [] } }return}func intersection2by2( []uint16, []uint16, []uint16,) int {iflen()*64 < len() {returnonesidedgallopingintersect2by2(, , ) } elseiflen()*64 < len() {returnonesidedgallopingintersect2by2(, , ) } else {returnlocalintersect2by2(, , ) }}// intersection2by2Cardinality computes the cardinality of the intersectionfunc intersection2by2Cardinality( []uint16, []uint16,) int {iflen()*64 < len() {returnonesidedgallopingintersect2by2Cardinality(, ) } elseiflen()*64 < len() {returnonesidedgallopingintersect2by2Cardinality(, ) } else {returnlocalintersect2by2Cardinality(, ) }}// intersects2by2 computes whether the two sets intersectfunc intersects2by2( []uint16, []uint16,) bool {if (len() == 0) || (len() == 0) {returnfalse }iflen()*64 < len() {returnonesidedgallopingintersect2by2Bool(, ) } elseiflen()*64 < len() {returnonesidedgallopingintersect2by2Bool(, ) } else {returnintersects2by2Bool(, ) }}func intersects2by2Bool( []uint16, []uint16,) bool { := 0 := 0 := [] := []:for {if < {for { ++if == len() {break } = []if >= {break } } }if < {for { ++if == len() {break } = []if >= {break } } } else {// (set2[k2] == set1[k1])returntrue } }returnfalse}func onesidedgallopingintersect2by2Bool( []uint16, []uint16,) bool { := 0 := 0 := [] := []:for {if < { = advanceUntil(, , len(), )if == len() {break } = [] }if < { ++if == len() {break } = [] } else {// (set2[k2] == set1[k1])returntrue } }returnfalse}func localintersect2by2( []uint16, []uint16, []uint16,) int {if (len() == 0) || (len() == 0) {return0 } := 0 := 0 := 0 = [:cap()] := [] := []:for {if < {for { ++if == len() {break } = []if >= {break } } }if < {for { ++if == len() {break } = []if >= {break } } } else {// (set2[k2] == set1[k1]) [] = ++ ++if == len() {break } = [] ++if == len() {break } = [] } }return}// / localintersect2by2Cardinality computes the cardinality of the intersectionfunc localintersect2by2Cardinality( []uint16, []uint16,) int {if (len() == 0) || (len() == 0) {return0 } := 0 := 0 := 0 := [] := []:for {if < {for { ++if == len() {break } = []if >= {break } } }if < {for { ++if == len() {break } = []if >= {break } } } else {// (set2[k2] == set1[k1]) ++ ++if == len() {break } = [] ++if == len() {break } = [] } }return}func advanceUntil( []uint16,int,int,uint16,) int { := + 1if >= || [] >= {return } := 1for + < && [+] < { *= 2 }varintif + < { = + } else { = - 1 }if [] == {return }if [] < {// means // array // has no // item // >= min // pos = array.length;return }// we know that the next-smallest span was too small += ( >> 1) := 0for +1 != { = ( + ) >> 1if [] == {return } elseif [] < { = } else { = } }return}func onesidedgallopingintersect2by2( []uint16, []uint16, []uint16,) int {if0 == len() {return0 } = [:cap()] := 0 := 0 := 0 := [] := []:for {if < { = advanceUntil(, , len(), )if == len() {break } = [] }if < { ++if == len() {break } = [] } else { [] = ++ ++if == len() {break } = [] = advanceUntil(, , len(), )if == len() {break } = [] } }return}func onesidedgallopingintersect2by2Cardinality( []uint16, []uint16,) int {if0 == len() {return0 } := 0 := 0 := 0 := [] := []:for {if < { = advanceUntil(, , len(), )if == len() {break } = [] }if < { ++if == len() {break } = [] } else { ++ ++if == len() {break } = [] = advanceUntil(, , len(), )if == len() {break } = [] } }return}func binarySearch( []uint16, uint16) int { := 0 := len() - 1for +16 <= { := int(uint32(+) >> 1) := []if < { = + 1 } elseif > { = - 1 } else {return } }for ; <= ; ++ { := []if >= {if == {return }break } }return -( + 1)}// searchResult provides information about a search request.// The values will depend on the context of the searchtype searchResult struct { value uint16 index int exactMatch bool}// notFound returns a bool depending the search context// For cases `previousValue` and `nextValue` if target is present in the slice// this function will return `true` otherwise `false`// For `nextAbsentValue` and `previousAbsentValue` this will only return `False`func ( *searchResult) () bool {return !.exactMatch}// outOfBounds indicates whether the target was outside the lower and upper bounds of the containerfunc ( *searchResult) () bool {return .index <= -1}// binarySearchUntil is a helper function around binarySearchUntilWithBounds// The user does not have to pass in the lower and upper bound// The lower bound is taken to be `0` and the upper bound `len(array)-1`func binarySearchUntil( []uint16, uint16) searchResult {returnbinarySearchUntilWithBounds(, , 0, len()-1)}// binarySearchUntilWithBounds returns a `searchResult`.// If an exact match is found the `searchResult{target, <index>, true}` will be returned, where `<index>` is// `target`s index in `array`, and `result.notFound()` evaluates to `false`.// If a match is not found, but `target` was in-bounds then the result.index will be the closest smaller value// Example: [ 8,9,11,12] if the target was 10, then `searchResult{9, 1, false}` will be returned.// If `target` was out of bounds `searchResult{0, -1, false}` will be returned.func binarySearchUntilWithBounds( []uint16, uint16, int, int) searchResult { := := -1if < [] {returnsearchResult{0, , false} }if > [] {returnsearchResult{0, len(), false} }for <= { := ( + ) / 2 := []if == {returnsearchResult{, , true} }if < {if > 0 && > [-1] {returnsearchResult{[-1], - 1, false} } = } else {if < && < [+1] {returnsearchResult{, , false} } = + 1 } }returnsearchResult{[], , false}}// binarySearchPast is a wrapper around binarySearchPastWithBounds// The user does not have to pass in the lower and upper bound// The lower bound is taken to be `0` and the upper bound `len(array)-1`func binarySearchPast( []uint16, uint16) searchResult {returnbinarySearchPastWithBounds(, , 0, len()-1)}// binarySearchPastWithBounds looks for the smallest value larger than or equal to `target`// If `target` is out of bounds a `searchResult` indicating out of bounds is returned// `target` does not have to exist in the slice.//// Example:// Suppose the slice is [...10,13...] with `target` equal to 11// The searchResult will have searchResult.value = 13func binarySearchPastWithBounds( []uint16, uint16, int, int) searchResult { := := -1if < [] {returnsearchResult{0, , false} }if > [] {returnsearchResult{0, len(), false} }for <= { := ( + ) / 2 := []if == {returnsearchResult{, , true} }if < {if > 0 && > [-1] {returnsearchResult{[], , false} } = } else {if < && < [+1] {returnsearchResult{[+1], + 1, false} } = + 1 } }returnsearchResult{[], , false}}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.